import Vue from "vue";

import VueRouter from 'vue-router'

Vue.use(VueRouter);

import axios from 'axios'
Vue.prototype.$axios=axios;
axios.defaults.baseURL='http://47.106.148.205:8899/'
axios.defaults.withCredentials=true;


const goodslist = () => import('../goods/goodslist.vue');
const goodinfo = () => import('../goods/goodInfo.vue');
const shopCart = () => import('../shopCar/shopCart.vue');
const order = () => import('../order/order.vue');
const login = () => import('../account/login.vue');
const payOrder = () => import('../pay/payOrder.vue');
const paySuccess = () => import('../pay/paySuccess.vue');
const vipCenter = () => import('../vip/vipCenter.vue');
const myOrder = () => import('../vip/myOrders.vue');
const orderInfo = () => import('../vip/orderInfo.vue');
const personCenter = () => import('../vip/personCenter.vue');

// import goodslist from '../goods/goodslist.vue';
// import goodinfo from '../goods/goodInfo.vue';
// import shopCart from '../shopCar/shopCart.vue';
// import order from '../order/order.vue';
// import login from '../account/login.vue';
// import payOrder from '../pay/payOrder.vue';
// import paySuccess from '../pay/paySuccess.vue';
// import vipCenter from '../vip/vipCenter.vue';
// import myOrder from '../vip/myOrders.vue';
// import orderInfo from '../vip/orderInfo.vue';
// import personCenter from '../vip/personCenter.vue';


const router = new VueRouter({
    routes: [
      { path: '/', component: goodslist },
      { path: '/site/goodslist', component: goodslist },
      { path: '/site/goodsinfo/:goodid', component: goodinfo },
      { path: '/site/shopCart/', component: shopCart },
      { path: '/site/login/', component: login },
      { path: '/site/order/:ids', component: order,meta:{needLogin:true} },
      { path: '/site/payOrder/:orderId', component: payOrder,meta:{needLogin:true} },
      { path: '/site/paySuccess', component: paySuccess,meta:{needLogin:true} },
      { 
        path: '/site/vipCenter', 
        component: vipCenter,meta:{needLogin:true} ,
        children:[
          {path:'',component:personCenter},
          
          {path:'personCenter',component:personCenter},
          { path: 'myOrder', component: myOrder,meta:{needLogin:true} },
          { path: 'orderInfo/:orderId', component: orderInfo,meta:{needLogin:true} }
        ]
    
      },


      
      
    ]
  })



  router.beforeEach((to, from, next) => {
    if(to.path!='/site/login/'){
      localStorage.setItem('lastVisitPath',to.path);
    }
    if(to.meta.needLogin){
      axios.get('site/account/islogin').then(response=>{
        if(response.data.code=='nologin'){
          router.push({path:'/site/login/'});
        }else {
          next();
        }
      })
    }else {
      next();
    }
  })

  export default router